﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Microsoft.Xna.Framework;

namespace WindowsGameLibrary1 {
    public class HeightMapInfoContent {
        public float[,] Height { get; private set; }
        public float TerrainScale { get; private set; }

        public HeightMapInfoContent(PixelBitmapContent<float> bitmap,
            float terrainScale, float terrainBumpiness) {
            TerrainScale = terrainScale;

            Height = new float[bitmap.Width, bitmap.Height];
            for (int y = 0; y < bitmap.Height; y++) {
                for (int x = 0; x < bitmap.Width; x++) {
                    Height[x, y] = (bitmap.GetPixel(x, y) - 1) * terrainBumpiness;
                }
            }
        }
    }

    [ContentTypeWriter]
    public class HeightMapInfoWriter : ContentTypeWriter<HeightMapInfoContent> {
        protected override void Write(ContentWriter output, HeightMapInfoContent value) {
            output.Write(value.TerrainScale);

            output.Write(value.Height.GetLength(0));
            output.Write(value.Height.GetLength(1));
            foreach (float height in value.Height) {
                output.Write(height);
            }
        }
         public override string GetRuntimeType(TargetPlatform targetPlatform) {
            return "WindowsGame5.HeightMapInfo, " +
                "WindowsGame5, Version=1.0.0.0, Culture=neutral";
        }
        public override string GetRuntimeReader(TargetPlatform targetPlatform) {
             return "WindowsGame5.HeightMapInfoReader, " +
                "WindowsGame5, Version=1.0.0.0, Culture=neutral";
        }
    }
}
